/****************************************************************************
 *
 * Copyright (C) 2003-2008 Los Alamos National Security, LLC
 *                         Packet Analytics Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.  You may not use, modify or
 * distribute this program under any other version of the GNU General
 * Public License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 ****************************************************************************/
package nfse.gui.config;

import com.google.gwt.user.client.ui.*;
import nfse.gui.query.TypeInfo;

public class SensorDialogBox extends DialogBox {

    final TextBox nameTextBox = new TextBox();
    
    ListBox typeComboBox = new ListBox();
    
    boolean cancelled = false;

    boolean isNew = false;

    public SensorDialogBox() {
        final SensorDialogBox note = this;
        
        FlexTable table = new FlexTable();
        
        
        typeComboBox.setVisibleItemCount(1);
        
        
        int idx = -1;

        table.setHTML(++idx, 0, "<b>Type</b>");
        table.setWidget(++idx, 0, typeComboBox);
        table.setHTML(++idx, 0, "<b>Sensor Name</b>");
        table.setWidget(++idx, 0, nameTextBox);
        table.setHTML(++idx, 0, "<i>Name cannot contain spaces, <br>puncuation or special characters.</i>");
        
        HorizontalPanel buttons = new HorizontalPanel();

        Button cancelButton = new Button();
        cancelButton.setText("Cancel");
        cancelButton.addClickListener(new ClickListener() {
            public void onClick(Widget sender) {
                cancelled = true;
                note.hide();

            }
        });

        Button okButton = new Button();
        okButton.setText("OK");
        okButton.addClickListener(new ClickListener() {
            public void onClick(Widget sender) {
                cancelled = false;
                note.hide();

            }
        });

        Button resetButton = new Button();
        resetButton.setText("Reset");
        resetButton.addClickListener(new ClickListener() {
            public void onClick(Widget sender) {
                reset();
            }
        });

        buttons.add(resetButton);
        buttons.add(okButton);
        buttons.add(cancelButton);

        table.setWidget(++idx, 0, buttons);
        this.setWidget(table);

    }

    public SensorInfo getSensorInfo() {
        SensorInfo info = new SensorInfo();
        info.sensorName = nameTextBox.getText().trim();
        info.type = typeComboBox.getValue(typeComboBox.getSelectedIndex());
        return info;
    }

    public void set(SensorInfo info) {
        nameTextBox.setText(info.sensorName);
        for (int i = 0; i < typeComboBox.getItemCount(); i++) {
            if (info.type.equals(typeComboBox.getValue(i))) {
                typeComboBox.setSelectedIndex(i);
                break;
            }
        }
    }
    
    public void setTypes(TypeInfo typeInfo) {
        typeComboBox.clear();
        int[] types = typeInfo.types;
        for (int i = 0; i < types.length; i++) {
            String typeName = typeInfo.getTypeName(types[i]);
            typeComboBox.addItem(typeName, "" + types[i]);
        }
        if (types.length > 0)
            typeComboBox.setSelectedIndex(0);
    }

    public boolean isCancelled() {
        return cancelled;
    }

    public boolean isNew() {
        return isNew;
    }

    public void setNew(boolean isNew) {
        this.isNew = isNew;
        if (isNew)
            reset();
    }
    
    private void reset() {
        nameTextBox.setText("");
        typeComboBox.setSelectedIndex(0);
    }
}
